package cn.simon.free.redis.jedis.string;

import cn.simon.free.redis.jedis.common.AbstractJedis;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * redis - string
 *
 * @author simon
 * @date 2021/5/14 10:42
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
@Component
public class JedisStringSupport extends AbstractJedis implements JedisString {

    @Override
    public void set(String key, String value) {
        super.redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public String get(String key) {
        return super.redisTemplate.opsForValue().get(key);
    }

    @Override
    public void setEx(String key, String value, long timeOut) {
        super.redisTemplate.opsForValue().set(key, value, timeOut);
    }

    @Override
    public Boolean setNx(String key, String value) {
        return super.redisTemplate.opsForValue().setIfAbsent(key, value);
    }

    /**
     * 原子操作,并设置过期时间
     *
     * @param key
     * @param value
     * @param timeOut
     * @param timeUnit
     */
    public Boolean setNxWithExpire(String key, String value, long timeOut, TimeUnit timeUnit) {
        return super.redisTemplate.opsForValue().setIfAbsent(key, value, timeOut, timeUnit);
    }
}
